package com.yanqu.road.dao.impl.activity.xiuxian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.xiuxian.data.CrossXiuXianPvpPlaceData;
import com.yanqu.road.entity.activity.xiuxian.data.CrossXiuXianPvpPlaceSeizeInfo;
import com.yanqu.road.entity.activity.xiuxian.data.CrossXiuXianPvpPosition;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXiuXianPvpPlaceDataDaoImpl extends TempDataObjectDao<CrossXiuXianPvpPlaceData> {

    public static String getCrossXiuXianPvpPlaceDataSql = "select * from `t_c_xiuxian_pvp_place_data` where `activityId`=?";

    public Map<Integer, CrossXiuXianPvpPlaceData> getCrossXiuXianPvpPlaceDataMap(int activityId) {
        Map<Integer, CrossXiuXianPvpPlaceData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossXiuXianPvpPlaceDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossXiuXianPvpPlaceData parse = parse(rs);
                map.put(parse.getPlaceId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertCrossXiuXianPvpPlaceDataSql = "insert into `t_c_xiuxian_pvp_place_data`(activityId,placeId,seizeInfoMap,updateTime,campId,seizeTime,lastSeizeRewardTime,lastSeizeCampId,lastNoticeSeizeTime) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossXiuXianPvpPlaceData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPlaceId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getSeizeInfoMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSeizeTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastSeizeRewardTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLastSeizeCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastNoticeSeizeTime()));
        if (getDb().executeNoneQuery(insertCrossXiuXianPvpPlaceDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossXiuXianPvpPlaceDataSql = "update `t_c_xiuxian_pvp_place_data` set seizeInfoMap=?,updateTime=?,campId=?,seizeTime=?,lastSeizeRewardTime=?,lastSeizeCampId=?,lastNoticeSeizeTime=? where activityId=? and placeId=?";

    @Override
    protected boolean updateData(CrossXiuXianPvpPlaceData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getSeizeInfoMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSeizeTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastSeizeRewardTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLastSeizeCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastNoticeSeizeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPlaceId()));
        if (getDb().executeNoneQuery(updateCrossXiuXianPvpPlaceDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossXiuXianPvpPlaceData parseData(ResultSet rs) throws SQLException {
        CrossXiuXianPvpPlaceData data = new CrossXiuXianPvpPlaceData();
        data.setActivityId(rs.getInt("activityId"));
        data.setPlaceId(rs.getInt("placeId"));
        data.setUpdateTime(rs.getLong("updateTime"));
        data.setSeizeInfoMap(JSON.parseObject(rs.getString("seizeInfoMap"), new TypeReference<ConcurrentHashMap<Integer, CrossXiuXianPvpPlaceSeizeInfo>>() {
        }));
        data.setCampId(rs.getInt("campId"));
        data.setSeizeTime(rs.getLong("seizeTime"));
        data.setLastSeizeRewardTime(rs.getLong("lastSeizeRewardTime"));
        data.setLastSeizeCampId(rs.getInt("lastSeizeCampId"));
        data.setLastNoticeSeizeTime(rs.getLong("lastNoticeSeizeTime"));
        return data;
    }
}
